package com.huilan.etl.message;

import com.huilan.etl.util.MessageUtil;
import com.huilan.etl.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.ServiceLoader;

/**
 * 消息接收定时任务
 *
 * @author ghy
 * @date 2019-01-03 15:51
 **/
@Slf4j
@Component
public class MessageReceive {

    /**
     * 每一秒执行一次
     */
    @Scheduled(fixedDelay = 1000)
    public void scheduled() throws Exception {
        log.info("=====>>>>定时任务执行");
        List<String> messageList = MessageUtil.receiveMessage();
        for(String message :messageList){
            String svcServiceCode = XmlUtil.getSvcServiceCode(message);
            ServiceLoader<IMessageService> serviceLoader = ServiceLoader.load(IMessageService.class);
            for (IMessageService messageService : serviceLoader){
                if(messageService.containsKey(svcServiceCode)){
                    messageService.excute(svcServiceCode,message);
                    break;
                }
            }
        }
    }
}
